package com.oracle.application.utils;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * ClassName: RSAUtilsNew <br/>
 * Description: <br/>
 * date: 2020/12/14 22:35<br/>
 *
 * @author zhaoran<br />
 * @since JDK 1.8
 */
public class RSAUtils {

    /** 安全服务提供者 */
    private static final Provider PROVIDER = new BouncyCastleProvider();

    /** 密钥大小 */
    private static final int KEY_SIZE = 2048;

    /**
     * 不可实例化
     */
    private RSAUtils() {
    }


    /**
     * 生成密钥对
     *
     * @return 密钥对
     */
    public static KeyPair generateKeyPair() {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER);
            keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
            return keyPairGenerator.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
       KeyPair keyPair=generateKeyPair();
       String publicKeyString= org.bouncycastle.util.encoders.Base64.toBase64String(keyPair.getPublic().getEncoded());
       String privateKeyString= org.bouncycastle.util.encoders.Base64.toBase64String(keyPair.getPrivate().getEncoded());
        System.out.println("publicKeyStr:"+publicKeyString);
        System.out.println("privateKeyStr:"+privateKeyString);
    }

    /**
     * 调用生成秘钥方法将公私钥转为字符串
     * */
    public  void createKeyPairToString(){
        KeyPair keyPair=generateKeyPair();
        String publicKeyString= org.bouncycastle.util.encoders.Base64.toBase64String(keyPair.getPublic().getEncoded());
        String privateKeyString= org.bouncycastle.util.encoders.Base64.toBase64String(keyPair.getPrivate().getEncoded());
        System.out.println("publicKeyStr:"+publicKeyString);
        System.out.println("privateKeyStr:"+privateKeyString);
    }

    /**
     * 将公钥字符串转为公钥
     * */
    public static PublicKey publicKeyToPublic(String publicKeyString){
        byte[] publicBytes = Base64.decodeBase64(publicKeyString);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
        KeyFactory keyFactory = null;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        try {
            PublicKey pubKey = keyFactory.generatePublic(keySpec);
            return pubKey;
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 将私钥字符串转为私钥
     * */
    public  static PrivateKey privateKeyStrToPrivate(String privateString){
        byte[] privateBytes = Base64.decodeBase64(privateString);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateBytes);
        KeyFactory keyFactory = null;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        try {
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            return privateKey;
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }
}